package com.woniuxy.reader.controller;


import com.woniuxy.reader.entity.Author;
import com.woniuxy.reader.entity.Book;
import com.woniuxy.reader.entity.Editor;
import com.woniuxy.reader.service.AuthorService;
import com.woniuxy.reader.service.BookService;
import com.woniuxy.reader.service.EditorService;
import com.woniuxy.reader.vo.AuthorVOO;
import com.woniuxy.reader.vo.BookVOO;
import com.woniuxy.reader.vo.EditorVO;
import com.woniuxy.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ss
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/editor")
@Api(tags = "编辑接口")
public class EditorController {

    @Autowired
    private BookService bookService;

    @Autowired
    private EditorService editorService;

    @Autowired
    private AuthorService authorService;

    @GetMapping
    @ApiOperation("查看所有作品")
    public Result selectAll(){
        return Result.success(bookService.selectAll());
    }

    @GetMapping("/select")
    @ApiOperation("筛选未签约作品")
    public Result selectNoSign(){
        List<Book> list = bookService.selectNoSign();
        if(list.size()==0){
            return Result.fail("404","没有任何作品未签约");
        }else{
            return Result.success(list);
        }
    }

    @RequestMapping("selectbyeditortel")
    @ApiOperation("查询自己签约的所有作者以及状态(editortel)")
    public Result selectSign(String editortel){
//        System.out.println("------------------------"+editortel);
        List<AuthorVOO> list = editorService.selectSignUp(editortel);
        if(list.size()==0){
            return Result.fail("404","该编辑未签约任何作者");
        }else{
            return Result.success(list);
        }
    }

    @GetMapping("/select/{id}")
    @ApiOperation("查看自己签约作者的所有作品数据(传入编辑id)")
    public Result selectData(@PathVariable("id") int id){
        List<Book> list = bookService.selectData(id);
        if(list.size()==0){
            return Result.fail("404","未查到任何作品");
        }else{
            return Result.success(list);
        }
    }

    @GetMapping("/selectauthor/{name}")
    @ApiOperation("查询指定作者的信息(传入要查询的作者笔名,模糊查询)")
    public Result selectAuthor(@PathVariable("name") String name){
        List<Author> list = authorService.selectAuthor(name);
        if(list.size()!=0){
            return Result.success(list);
        }else{
            return Result.fail("404","该作者不存在");
        }
    }

    @PostMapping("{bookid}")
    @ApiOperation("禁书(传入书籍id)")
    public Result updateBook(@PathVariable("bookid") int bookid){
        bookService.updateBook(bookid);
        return Result.success("禁书成功");
    }

    @GetMapping("/selectSign")
    @ApiOperation("查看正在申请签约的作者")
    public Result selectSigning(){
        List<BookVOO> list = authorService.selectSigning();
        if(list.size()!=0){
            return Result.success(list);
        }else{
            return Result.fail("404","没有正在申请签约的作者");
        }
    }

    @PostMapping("/updateflag/{bookid}")
    @ApiOperation("申精作品(传入书籍id)")
    public Result updateBookFlag(@PathVariable("bookid") int bookid){
        bookService.updateBookFlag(bookid);
        return Result.success("申精成功");
    }

    @RequestMapping("agree")
    @ApiOperation("同意签约申请")
    public Result agreeSgin(int authorid,String editortel){
        System.out.println("-----------------------------签约");
        Editor editor = editorService.selectByTel(editortel);
        return Result.success(authorService.SginAuthor(authorid,editor.getEditorId()));

    }

    @RequestMapping("reject")
    @ApiOperation("驳回签约申请")
    public Result rejectSgin(int authorid){
        System.out.println("-----------------------------驳回签约");
        return Result.success( editorService.rejectSgin(authorid));
    }
}

